package com.source.module_kotlin_library.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Build
import android.support.annotation.RequiresApi
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View

class MyButton : View {
    private var paint: Paint
    private var fontPain: Paint
    private val textStr = "点击按钮"

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    init {
        paint = Paint()
        paint.isAntiAlias = true

        fontPain = Paint()
        fontPain.style = Paint.Style.FILL
        fontPain.color = Color.WHITE
        fontPain.textAlign = Paint.Align.CENTER
        fontPain.textSize = 50f
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onDraw(canvas: Canvas?) {
        canvas!!.drawRoundRect(0f, 0f, width.toFloat(), height.toFloat(), 25f, 25f, paint)
        canvas!!.drawText(textStr, width / 2.toFloat(), height / 2.toFloat(), fontPain)
    }

    override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
        return super.dispatchTouchEvent(event)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return super.onTouchEvent(event)
    }
}